<?php
/* wwwschool project

Copyright (C) 2009  Phillip Aldridge
Email : info@imi21.com
Web site http://www.imi21.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/**
 *  File : editAddress.php
 *  called only via ajax 
 *  
 *requires POST  
 */  
 //list=users&amp;level=headmasters

//$$TODO Who is allowed to edit this

require '../../includes/classes/WS3formview.php';


$date=date("Y-m-d H:i:s");
$title="";
/**
 * get user name to display
 */
  
$db=new WS3_Database();
$q="SELECT first_name,last_name FROM `user` WHERE 1 AND `id`= '$id' LIMIT 1";


if ($db->query($q) && $db->nextRecord() )
{
  $title=$db->f('first_name')." ".$db->f('last_name');
}
  
/**
 * Check to see if there is an address
 * If false, then create an empty one 
 **/


$user_id=$id;

$db=new WS3_Database();
$q="SELECT id FROM `addressbook` WHERE 1 AND `user_id`= '$user_id' LIMIT 1";


if ( $db->query($q) )
{
  if ( $db->numberRows()==0 )
  {
    $db2=new WS3_Database();
    $q="INSERT INTO `addressbook` (`id`,`date_created`,`user_id` ) VALUES (NULL,'$date',$user_id)";
    $db2->query($q); 
    $id=$db2->insertID();  

  }elseif ( $db->nextRecord() )
  {
    $id=$db->f('id');
  }
}


// get the address 
$db=new WS3_Database();
$q="SELECT * FROM `addressbook` WHERE 1 AND `id`= '$id' LIMIT 1";

if ($db->query($q) && $db->nextRecord() )
{ // user does exisits

  $message='';
  $form = new WS3_FormView('main');
  $form->setTitle( "Edit Address :$title" );
  $form->displayResetButton( true );
  $form->setAction( 'update' );
  $form->addHiddenField( 'f', 'Address' ); // which function to call
  $form->addHiddenField( 'function2', 'list' ); // which function to call after update
  $form->addHiddenField( 'param1', 'Users' ); // parameter1 for post
  $form->addHiddenField( 'id', $db->f('id') );
  
  /*$selected= $db->f('user_type');
  $options=$_mUSER;
  $form->addSelect( 'User type', 'user_type', 'user_type', $options, $selected );
 */ 
 $form->addFieldSet( 'address', 'Address');
  $form->addTextField( 'address', 'Street1','street1','street1',$db->f('street1') );
  $form->addTextField( 'address', 'Street2','street2','street2',$db->f('street2'));
  $form->addTextField( 'address', 'Town','town','town',$db->f('town'));
  $form->addTextField( 'address', 'Postcode','postcode','postcode',$db->f('postcode'));
  $form->addTextField( 'address', 'Country','country','country',$db->f('country'));

  echo $form->display();
  
  $message="Editing";
}else{
  $message="Error : ID not found";
}

echo "document.getElementById('messages').innerHTML='$message';";
if (W3DEBUG)  $debug=new WS3_Debug();
?>